package com.clean.cleantotal

import android.util.Log
import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.clean.cleantotal.base.BaseActivity
import com.clean.cleantotal.databinding.ActivityMainBinding
import com.clean.cleantotal.compores.HomeFragment
import com.clean.cleantotal.compores.MineFragment
import com.clean.cleantotal.compores.ToolsFragment
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationView
import com.google.firebase.messaging.FirebaseMessaging

class MainActivity : BaseActivity<ActivityMainBinding>() {
    override val viewBinding: ActivityMainBinding
        get() = ActivityMainBinding.inflate(layoutInflater)
    private var fragmentList = ArrayList<Fragment>()
    override fun initView() {
        mBinding.viewPager.isUserInputEnabled = false
        mBinding.navMain.itemIconTintList = null

        fragmentList.add(HomeFragment())
        fragmentList.add(ToolsFragment())
        fragmentList.add(MineFragment())

        mBinding.viewPager.adapter = object : FragmentStateAdapter(this) {
            override fun createFragment(position: Int): Fragment {
                return fragmentList[position]
            }

            override fun getItemCount(): Int {
                return fragmentList.size
            }
        }

        mBinding.navMain.setOnNavigationItemSelectedListener(object :
            NavigationView.OnNavigationItemSelectedListener,
            BottomNavigationView.OnNavigationItemSelectedListener {
            override fun onNavigationItemSelected(item: MenuItem): Boolean {
                when (item.itemId) {
                    R.id.tab_home -> {
                        mBinding.viewPager.setCurrentItem(0, false)
                    }

                    R.id.tab_tools -> {
                        mBinding.viewPager.setCurrentItem(1, false)
                    }

                    R.id.tab_mine -> {
                        mBinding.viewPager.setCurrentItem(2, false)
                    }
                }
                return true
            }
        })


        FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
            if (!task.isSuccessful) {
                Log.e("aaa", "Fetching FCM registration token failed", task.exception)
                return@OnCompleteListener
            }

            // Get new FCM registration token
            val token = task.result
            Log.e("aaa", "initView:  令牌 -- ${token}" )
            // Log and toast
        })

    }

}